layui.use(['tree','table','treeGrid','layer','jquery'],function(){

        $ = layui.$;
    var layer = parent.layer === undefined ? layui.layer : top.layer;
    var table = layui.table;


    //列表数据
    var option = {
        elem: '#tableList',
        url : '/admin/facilityUsed/bookedList',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limits : [10,15,20,25],
        limit : 15,
        id : "listTable",
        loading:true,
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'facilityId', title: '仪器编号', align:'center',minWidth:150},
            // {field: 'facilityName', title: '仪器名称', align:'center',minWidth:150,templet:function (d){
            //         var name = null;
            //         $.ajax({
            //             url:"/admin/facility/getFacility?facilityId="+d.facilityId,
            //             type:'GET',
            //             async:false,
            //             success:function (data){
            //                 name = data.facilityName;
            //             }
            //         });
            //         return name;
            //
            // }},
            {field: 'userId', title: '使用人', align:'center'},
            {field: 'useDate', title: '预约时间', align:'center',minWidth:300,sort:true, templet:function(d){
                    //处理时间
                    var dateS = new Date(d.useDate);
                    var dateE=new Date(d.returnDate);

                    var usetime=dateS.getFullYear() + '-' + (dateS.getMonth() + 1) + '-' + dateS.getDate()+ " " + dateS.getHours() + ":" + dateS.getMinutes() + ":" + dateS.getSeconds()
                    +" -- "+dateE.getFullYear() + '-' + (dateE.getMonth() + 1) + '-' + dateE.getDate()+ " " + dateE.getHours() + ":" + dateE.getMinutes() + ":" + dateE.getSeconds();
                    return usetime;
                }},
            {field: 'actualUseTime', title: '归还时间', align:'center',minWidth:180,sort:true},
            {field: 'afterStatus', title: '使用后状态',sort:true,minWidth:150, align:'center',templet(d){

                return d.afterStatus=="0"?"正常":d.afterStatus=="1"?"报废":"";
                }},
            {field: '88',title: '操作', minWidth:175, templet:'#tableListBar',fixed:"right",align:"center"}
        ]],
        done: function(res, curr, count) {

        }
    };




    //场景数据
    var height = $(document).height();
    $(".orgbox").css('height',(height - 40) + "px");
    $(window).on("resize",function () {
        $(".orgbox").css('height',(height - 40) + "px");
    });
    var treeData = {
        elem: '#tree' //指定元素
        , target: '_blank' //是否新选项卡打开（比如节点返回href才有效）
        , click: function (item) { //点击节点回调
            option.url = '/admin/facilityUsed/getBookedListByCatId';
            option.where = {categoryId: item.id};
            table.render(option);
        },
        nodes:null
    };

    $.get('/admin/facilityCategory/menu',function(data) {
        treeData.nodes = data.data;
        layui.tree(treeData);
    });

    //设备列表
    var tableIns = table.render(option);

    //搜索设备信息
    $(".search_btn").on("click",function(){
        var searchKey = $(".searchVal").val();
        if(searchKey != ''){
            option.url = '/admin/facilityUsed/search';
            option.method = 'post';
            option.where = {keyword: searchKey};
            table.render(option);
        }else{
            layer.msg("请输入搜索的内容");
        }
    });


    //添加设备
    $(".addBtn").click(function(){
        var index = layui.layer.open({
            title : "使用记录添加",
            type : 2,
            content : "facilityAppointAdd.jsp",
            area: ['100%',$(document).height() + 'px'],
            success : function(){
                //这里可以有写成功后的其他操作
                setTimeout(function(){
                    layui.layer.tips('点击此处返回记录列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        });
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        });
    });



    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('listTable'),//此处的id为table数据格式中的id
            data = checkStatus.data,
            idArr = [];
        if(data.length > 0) {
            for (var i in data) {
                idArr.push(data[i].id);
            }
            layer.confirm('确定归还选中的设备？', {icon: 3, title: '提示信息'}, function (index) {
                $.get("/admin/facilityUsed/batchBookReturn",{
                    ids : idArr  //将需要删除的newsId作为参数传入
                },function(data){
                    if (data.code == 0) {
                        layer.msg("归还成功！");
                    } else {
                        layer.msg("归还失败，请重试！");
                    }
                    tableIns.reload();
                    layer.close(index);
                })
            })
        }else{
            layer.msg("请选择需要归还的设备");
        }
    });


    //列表操作
    table.on('tool(tableList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        // console.log($('#edit').hasClass('layui-btn-disabled'));
        if(layEvent === 'edit' && $('#edit').hasClass('layui-btn-disabled') === false){ //归还
            var index = layui.layer.open({
                title : "确认仪器状态",
                type : 2,
                resize:false,
                shadeClose:true,
                content : "/admin/views/appoint/facilityAppointEdit.jsp?id="+data.id+"&facilityId=" + data.facilityId+"&status="+data.afterStatus,
                area: ['350px','150px']
            });

            //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
            $(window).on("resize",function(){
                layui.layer.full(index);
            });

        }

    });

});